﻿using HAF.FoundationLib;
using System;
using System.Collections.Generic;
using WareDealer.Helper;
using WareDealer.Mode;

namespace WareDealer.Works
{
    /// <summary>
    /// 历史数据导入工作项
    /// </summary>
    public class ImportHisDataWork : ProcessItem
    {
        private string _connstr = "";
        public override void ExecItem()
        {
            if (string.IsNullOrEmpty(Name)) return;
            if (!System.IO.File.Exists(Name)) return;
            
            _connstr = Name;
            _iStep = 1;
            InitAutoProcess("main", 4);
            ShowAutoMessage("历史数据导入开始！");
            ImportHisData();
            EndAutoProcess("main", true);
            ShowAutoMessage("历史数据导入完成！");
        }

        List<ProductInfo> _hisWares = new List<ProductInfo>();
        List<ProductInfo> _curWares = new List<ProductInfo>();

        List<PriceWebHistory> _hisPrices = new List<PriceWebHistory>();
        //List<PriceWebHistory> _curPrices = new List<PriceWebHistory>();
 
        private int _iStep = 1;
        /// <summary>
        /// 
        /// </summary>
        private void ImportHisData()
        {
            try
            {
                GetWareHisData();
                ImportWareHisData();

                GetWareHisPrices();
                //GetWareHisPricesDb();
                ImportWareHisPrice();
            }
            catch (Exception ex)
            {
                Faster.XLogErr(ex.Message);
            }
            
        }
        /// <summary>
        /// 获取历史商品数据
        /// </summary>
        private void GetWareHisData()
        {
            ShowAutoMessage("获取历史商品数据开始！");
            _hisWares = DbHelper.GetInstance().GetHisWareData(_connstr);
            _curWares = DbHelper.GetInstance().GetAllWares();
            //清理历史数据
            ShowAutoMessage("清理历史数据！");
            foreach (var item in _curWares)
            {
                _hisWares.RemoveAll(t => t.ProductID == item.ProductID);
            }
            ShowAutoMessage("历史数据清理完成！");
            ShowAutoStep("main", _iStep++);
            ShowAutoMessage("获取历史商品数据完成！");
        }
        /// <summary>
        /// 历史商品数据导入
        /// </summary>
        private void ImportWareHisData()
        {
            ShowAutoMessage(string.Format("历史商品数据导入开始，共计需要导入{0}条历史商品数据！",_hisWares.Count));
            if (_hisWares.Count > 0)
            {
                DbHelper.GetInstance().ImportHisWareData(_hisWares);
            }
            //_iStep++;
            ShowAutoStep("main", _iStep++);
            ShowAutoMessage("历史商品数据导入完成！");
        }
        /// <summary>
        /// 获取商品历史价格
        /// </summary>
        private void GetWareHisPricesDb()
        {
            ShowAutoMessage("获取历史商品价格开始！");
            _hisPrices = DbHelper.GetInstance().GetHisWarePrice(_connstr, _hisWares);
            ShowAutoStep("main", _iStep++);
            ShowAutoMessage("获取历史商品价格完成！");
        }
        /// <summary>
        /// 获取商品历史价格
        /// </summary>
        private void GetWareHisPrices()
        {
            ShowAutoMessage("获取历史商品价格开始！");
            if (_hisWares.Count > 0)
            {
                _hisPrices.Clear();
                List<PriceWebHistory> curHisPrices =  DbHelper.GetInstance().GetHisWarePrice(_connstr);
                if (curHisPrices != null && curHisPrices.Count > 0)
                {
                    curHisPrices.RemoveAll(t => t.PriceDate < DateTime.Parse("2015-01-01"));
                    ShowAutoMessage("清理历史商品价格数据！");
                    foreach (var item in _hisWares)
                    {
                        _hisPrices.AddRange(curHisPrices.FindAll(t => t.PID == item.ProductID));
                        //_curHisPrices.RemoveAll(t => t.PID == item.ProductID);
                    }
                    ShowAutoMessage("清理历史商品价格数据完成！");
                }
            }
            ShowAutoStep("main", _iStep++);
            ShowAutoMessage("获取历史商品价格完成！");
        }
        /// <summary>
        /// 历史商品价格导入
        /// </summary>
        private void ImportWareHisPrice()
        {
            ShowAutoMessage(string.Format("历史商品价格导入开始，共计需导入{0}条历史商品价格！", _hisPrices.Count));
            if (_hisPrices.Count > 0)
            {
                DbHelper.GetInstance().ImportHisWarePrice(_hisPrices);
            }
            ShowAutoStep("main", _iStep++);
            ShowAutoMessage("历史商品价格导入完成！");
        }

    }
}
